<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="所属校区" prop="schoolId">
        <treeselect v-model="queryParams.schoolId" :options="deptOptions" :normalizer="normalizer" placeholder="选择所属校区" style="width:215px"/>
      </el-form-item>
      <el-form-item label="学生" prop="studentId">
        <el-select filterable v-model="queryParams.studentId" placeholder="请选择学生" clearable size="small">
          <el-option v-for="dict in studentXYList" :key="dict.userId" :label="dict.userName+':'+dict.phonenumber" :value="dict.userId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="操作老师" prop="teacherId">
        <el-select filterable v-model="queryParams.teacherId" placeholder="请选择操作老师" clearable size="small">
          <el-option v-for="dict in teacherList" :key="dict.userId" :label="dict.userName+':'+dict.phonenumber" :value="dict.userId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="缴费时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:payrecord:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:payrecord:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:payrecord:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:payrecord:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleUpdateHk"
          v-hasPermi="['business:payrecord:editHk']"
        >耗课</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="payrecordList" ref="table" @selection-change="handleSelectionChange" row-key="studentId" :expand-row-keys="expandKeys" @expand-change="rowExpand" @sort-change="sortChange">
      <el-table-column type="expand" prop="">
        <template slot-scope="">
          <el-table :data="consumeData">
            <el-table-column label="所属校区" align="left" prop="schoolId" :formatter="getSchool" :show-overflow-tooltip="true"/>
            <el-table-column label="所属班级" align="left" prop="classesId" :formatter="getClasses" :show-overflow-tooltip="true"/>
            <el-table-column label="学生" align="left" prop="studentId" :formatter="getStudentXY" :show-overflow-tooltip="true"/>
            <el-table-column label="耗课金额" align="right" prop="consumeAmount" width="150"/>
            <el-table-column label="操作老师" align="left" prop="teacherId" width="300" :formatter="getOpt" :show-overflow-tooltip="true"/>
            <el-table-column label="耗课时间" align="center" prop="createTime" width="160">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属校区" align="left" prop="schoolId" :formatter="getSchool" :show-overflow-tooltip="true"/>
      <el-table-column label="学生姓名" align="left" prop="xsname" :show-overflow-tooltip="true"/>
      <el-table-column label="学生电话" align="center" prop="xsphone" width="110"/>
      <el-table-column label="业务金额" align="right" prop="amount" sortable="custom" width="100"/>
      <el-table-column label="购买次数" align="right" prop="num" sortable="custom" width="100"/>
      <el-table-column label="剩余业务金额" align="right" prop="remainder" sortable="custom" width="130"/>
      <el-table-column label="剩余次数" align="right" prop="remaindnum" sortable="custom" width="100"/>
      <el-table-column label="实缴金额" align="right" prop="realAmount" sortable="custom" width="100"/>
      <el-table-column label="操作老师" align="left" prop="teacherId" width="200" :formatter="getOpt" :show-overflow-tooltip="true"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150" sortable="custom">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:payrecord:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:payrecord:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateMx(scope.row)"
            v-hasPermi="['business:payrecordd:list']"
          >明细</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateHk(scope.row)"
            v-hasPermi="['business:payrecord:editHk']"
          >耗课</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改缴费记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="所属校区" prop="schoolId">
              <treeselect v-model="form.schoolId" :options="deptOptions" :normalizer="normalizer" placeholder="选择所属校区" v-if="this.form.id!=null" :disabled="true"/>
              <treeselect v-model="form.schoolId" :options="deptOptions" :normalizer="normalizer" placeholder="选择所属校区" v-if="this.form.id==null"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="业务金额" prop="amount">
              <el-input v-model="form.amount" placeholder="请输入业务金额" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="购买次数" prop="num">
              <el-input v-model="form.num" placeholder="请输入购买次数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="折扣率" prop="reduction">
              <el-select filterable v-model="form.reduction" placeholder="请选择折扣率" clearable size="small" style="width:180px" @change="setRealAmount">
                <el-option v-for="dict in reductions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实缴金额" prop="realAmount">
              <el-input v-model="form.realAmount" placeholder="请输入实缴金额" style="width:180px"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="this.form.id==null?'选择现有学生':'学生姓名'" prop="studentId">
              <el-input v-model="form.xsname" v-if="this.form.id!=null" readonly/>
              <el-select filterable v-model="form.studentId" placeholder="请选择学生" clearable size="small" style="width:460px" v-if="this.form.id==null" @change="selectStudent">
                <el-option label="没有可选择的学生" value="0"></el-option>
                <el-option v-for="dict in studentXYList" :key="dict.userId" :label="dict.userName+(dict.phonenumber!=null?':'+dict.phonenumber:'')" :value="dict.userId"></el-option>                
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="this.form.id==null&&this.showNewStudent==true">
          <el-col :span="12">
            <el-form-item label="新学生姓名" prop="studentName">
              <el-input v-model="form.studentName" placeholder="请输入学生姓名" style="width:180px"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="新学生性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择">
                <el-option v-for="dict in sexOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="this.form.id==null&&this.showNewStudent==true">
          <el-col :span="12">
            <el-form-item label="所属班级" prop="classesId">
              <el-select filterable v-model="form.classesId" placeholder="请选择所属班级" clearable size="small">
                <el-option v-for="dict in classesList" :key="dict.id" :label="dict.classesName" :value="dict.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="监护人电话" prop="phonenumber">
              <el-input v-model="form.phonenumber" placeholder="请输入监护人电话" style="width:180px"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="电话" prop="xsphone" v-if="this.form.id!=null">
              <el-input v-model="form.xsphone" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="titleMx" :visible.sync="openMx" width="900px" append-to-body>
      <div class="sketch_content">
        <ul>
          <el-button icon="el-icon-document" size="mini" @click="cancelMx" style="margin-left:-30px;">取消</el-button>
          <el-table v-loading="loadingMx" :data="mxList" style="margin-left:-30px;">        
            <el-table-column label="所属校区" align="center" prop="schoolId" :formatter="getSchool" width="150" :show-overflow-tooltip="true"/>
            <el-table-column label="学生" align="center" prop="studentName" :formatter="showCurStudentName" :show-overflow-tooltip="true"/>
            <el-table-column label="原业务金额" align="center" prop="oldamount" width="100"/>
            <el-table-column label="业务金额" align="center" prop="amount" width="100"/>
            <el-table-column label="原实缴金额" align="center" prop="realOldamount" width="100"/>
            <el-table-column label="实缴金额" align="center" prop="realAmount" width="100"/>
            <el-table-column label="备注" align="center" prop="remark" width="100" :show-overflow-tooltip="true"/>
            <el-table-column label="记录老师" align="center" prop="teacherId" :formatter="getOpt" :show-overflow-tooltip="true"/>
          </el-table>
          <pagination
            v-show="totalMx>0"
            :total="totalMx"
            :page.sync="queryParamsMx.pageNum"
            :limit.sync="queryParamsMx.pageSize"
            @pagination="getListMx"
          />
        </ul>
      </div>      
    </el-dialog>
    <el-dialog :title="titleHk" :visible.sync="openHk" width="500px" append-to-body>
      <el-form ref="formHk" :model="formHk" :rules="rulesHk" label-width="80px">
        <el-form-item label="所属校区" prop="schoolId">
          <treeselect v-model="formHk.schoolId" :options="deptOptions" :normalizer="normalizer" placeholder="选择所属校区" :disabled="true"/>
        </el-form-item>
        <el-form-item label="学生" prop="studentId">
          <el-select filterable v-model="formHk.studentId" placeholder="请选择学生" clearable size="small" :disabled="true">
            <el-option v-for="dict in studentXYList" :key="dict.userId" :label="dict.userName+(dict.phonenumber!=null?':'+dict.phonenumber:'')" :value="dict.userId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属班级" prop="classesId">
          <el-select filterable v-model="formHk.classesId" placeholder="请选择所属班级" clearable size="small">
            <el-option v-for="dict in classesHkList" :key="dict.id" :label="dict.classesName" :value="dict.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="耗课金额" prop="consumeAmount">
          <el-input v-model="formHk.consumeAmount" placeholder="请输入耗课金额" />
        </el-form-item>        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormHk">确 定</el-button>
        <el-button @click="cancelHk">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="titleHkIds" :visible.sync="openHkIds" width="500px" append-to-body>
      <el-form ref="formHkIds" :model="formHkIds" :rules="rulesHk" label-width="80px">
        <el-form-item label="所属校区" prop="schoolId">
          <treeselect v-model="formHkIds.schoolId" :options="deptOptions" :normalizer="normalizer" placeholder="选择所属校区" :disabled="true"/>
        </el-form-item>
        <el-form-item label="耗课金额" prop="consumeAmount">
          <el-input v-model="formHkIds.consumeAmount" placeholder="请输入耗课金额" />
        </el-form-item>
        <el-form-item label="所属班级" prop="classesId">
          <el-select filterable v-model="formHkIds.classesId" placeholder="请选择所属班级" clearable size="small">
            <el-option v-for="dict in classesHkList" :key="dict.id" :label="dict.classesName" :value="dict.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学生" prop="studentId">
          <el-select filterable v-model="formHkIds.studentId" placeholder="请选择学生" clearable size="small" multiple>
            <el-option v-for="dict in studentXYIdList" :key="dict.studentId" :label="dict.xsUsername+':'+dict.xsPhone" :value="dict.studentId"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormHkIds">确 定</el-button>
        <el-button @click="cancelHkIds">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPayrecord, getPayrecord, delPayrecord, addPayrecord, updatePayrecord, exportPayrecord } from "@/api/business/payrecord";
import { listPayrecordd, getPayrecordd, delPayrecordd, addPayrecordd, updatePayrecordd, exportPayrecordd } from "@/api/business/payrecordd";
import { listDept } from "@/api/system/dept";
import { listClasses, getClasses, getClassesIds } from "@/api/business/classes";
import { listStudentXY, listOpt } from "@/api/system/user";
import { listConsume, getConsumeHk, getConsumeHkByIds, addConsume } from "@/api/business/consume";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Payrecord",
  components: {
    Treeselect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      loadingMx: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totalMx: 0,
      // 缴费记录表格数据
      payrecordList: [],
      mxList: [],
      // 弹出层标题
      title: "",
      titleHk: "",
      titleMx: "",
      titleHkIds: "",
      curStudentName: "",
      // 日期范围
      dateRange: [],
      // 是否显示弹出层
      open: false,
      openHk: false,
      openMx: false,
      openHkIds: false,
      showNewStudent: false,
      reductions: [],
      yesNo: [],
      consumeData: [],
      expandKeys:[],
      deptOptions: [],//学校树选项
      schoolList: [],//学校列表
      classesList: [],//班级列表
      studentXYList: [],//学生列表
      studentXYIdList: [],//学生列表
      teacherList: [],//操作老师列表
      classesHkList: [],//班级列表
      sexOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolId: null,
        studentId: null,
        xsname: null,
        xsphone: null,
        amount: null,
        num: null,
        teacherId: null,
      },
      queryParamsMx: {
        pageNum: 1,
        pageSize: 10,
        schoolId: null,
        userId: null
      },
      // 表单参数
      form: {},
      formHk: {},
      formHkIds: {},
      // 表单校验
      rules: {
        amount: [
          { required: true, message: "缴费金额不能为空", trigger: "blur" }
        ],
        num: [
          { required: true, message: "购买次数不能为空", trigger: "blur" }
        ],
        phonenumber: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
      },
      rulesHk: {
        consumeAmount: [
          { required: true, message: "耗课金额不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_yes_no").then(response => {
      this.yesNo = response.data;
    });
    for (let i = 100; i >=1; i--) {
      this.reductions.push({dictValue:i/100, dictLabel:i+'%'});
    }
    this.getDicts("sys_user_sex").then(response => {
      this.sexOptions = response.data;
    });
    listDept().then(response => {
        this.schoolList = response.data;
	      this.deptOptions = this.handleTree(response.data, "deptId");
    });
    listClasses().then(response => {
        this.classesList = response.rows;
    });
    listStudentXY().then(response => {
        this.studentXYList = response.rows;
    });
    listOpt().then(response => {
        this.teacherList = response.rows;
    });
  },
  watch: {
    'formHk.classesId': 'currClassesChange',
    'formHkIds.classesId': 'currIdsClassesChange',
    'form.schoolId': 'currSchoolChange'
  },   
  methods: {
    currClassesChange(val) {
      if (val!=null) {
        getClasses(val).then(response => {
          this.formHk.consumeAmount = response.data.amount;
        });
      }
    },
    currIdsClassesChange(val) {
      if (val!=null) {
        this.formHkIds.studentId = [];
        this.studentXYIdList = [];
        getClassesIds(val).then(response => {
          this.formHkIds.consumeAmount = response.data.amount;
          this.studentXYIdList = response.data.studentList;
          let xsIds = []
          response.data.studentList.forEach(item =>{
            xsIds.push(item.studentId);  
          })
          this.formHkIds.studentId = xsIds;
        });
      }
    },
    currSchoolChange(val) {
      let params = {schoolId: val};
      listClasses(params).then(response => {
        this.classesList = response.rows;
      });      
    },
    setClasses() {
      var Inp = document.getElementsByTagName("input");
      for (var i = 0; i < Inp.length; i++) {
        Inp[i].style.backgroundColor = '#FFFFFF'  
      }
    },
    selectStudent(val) {      
      if (val==='0') {
        this.showNewStudent = true;
      }else {
        this.showNewStudent = false;
      }
    },
    /** 转换学校数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    getSchool(row) {
      for (let i in this.schoolList) {
        if (this.schoolList[i].deptId === row.schoolId) {
          return this.schoolList[i].deptName;
        }
      }
    },
    getClasses(row) {
      for (let i in this.classesList) {
        if (this.classesList[i].id === row.classesId) {
          return this.classesList[i].classesName;
        }
      }
    },
    getStudentXY(row) {
      for (let i in this.studentXYList) {
        if (this.studentXYList[i].userId === row.studentId) {
          return this.studentXYList[i].userName+':'+this.studentXYList[i].phonenumber;
        }
      }
    },
    getOpt(row) {
      for (let i in this.teacherList) {
        if (this.teacherList[i].userId === row.teacherId) {
          return this.teacherList[i].userName+':'+this.teacherList[i].phonenumber;
        }
      }
    },
    showCurStudentName() {
      return this.curStudentName;
    },
    async rowExpand(row, expandRows) {
      if(this.expandKeys.indexOf(row.studentId) >= 0) {
        this.expandKeys.shift()
        return;
      }
      //恢复默认值
      this.consumeData = [];
      let detailParam = {schoolId: row.schoolId, studentId: row.studentId};
      await listConsume(detailParam).then(response => {
        this.consumeData = response.rows;
        if (JSON.stringify(this.consumeData) == '{}'){
          this.expandKeys.shift()
          this.expandKeys.push(row.studentId)
	      }else{
		      row["vendor"] = this.consumeData["vendor"]
		      this.expandKeys.shift()
		      this.expandKeys.push(row.studentId)
	      }
      });
      if (expandRows.length >1){
	      expandRows.shift()
	    }
    },
    sortChange(column) {
      this.loading = true;
      if (column.prop === 'amount') {
        this.queryParams.order = 'amount';
      } else if (column.prop === 'num') {
        this.queryParams.order = 'num';
      } else if (column.prop === 'remainder') {
        this.queryParams.order = 'remainder';      
      } else if (column.prop === 'createTime') {
        this.queryParams.order = 'create_time';
      }
      if (column.order === 'ascending') {
        this.queryParams.orderType = 'asc';
      } else if (column.order === 'descending') {
        this.queryParams.orderType = 'desc';
      }      
      listPayrecord(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.payrecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询缴费记录列表 */
    getList() {
      this.loading = true;
      listPayrecord(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.payrecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getListMx() {
      this.loadingMx = true;
      listPayrecordd(this.queryParamsMx).then(response => {
        this.mxList = response.rows;
        this.totalMx = response.total;
        this.loadingMx = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelMx() {
      this.openMx = false;      
    },
    cancelHk() {
      this.openHk = false;
      this.resetHk();
    },
    cancelHkIds() {
      this.openHkIds = false;
      this.resetHkIds();
    },
    setRealAmount(val) {
      this.form.realAmount = this.form.amount*val;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        schoolId: null,
        studentId: null,
        xsname: null,
        xsphone: null,
        amount: null,
        num: null,
        teacherId: null,
        createTime: null
      };
      this.resetForm("form");
    },
    resetHk() {
      this.formHk = {
        id: null,
        schoolId: null,
        classesId: null,
        studentId: null,
        consumeAmount: null,
        teacherId: null,
        createTime: null,
        remainder: null,
      };
      this.resetForm("formHk");
    },
    resetHkIds() {
      this.formHkIds = {
        id: null,
        schoolId: null,
        classesId: null,
        studentId: null,
        consumeAmount: null,
        teacherId: null,
        createTime: null,
        remainder: null,
      };
      this.resetForm("formHkIds");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加缴费记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPayrecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改缴费记录";
      });
    },
    handleUpdateMx(row) {
      this.queryParamsMx.schoolId = row.schoolId;
      this.queryParamsMx.userId = row.userId;
      this.curStudentName = row.xsname;
      this.getListMx();
      this.openMx = true;
      this.titleMx = "缴费明细信息";
    },
    handleUpdateHk(row) {      
      const id = row.id || this.ids
      if (row.remainder>0) {
        this.resetHk();
        getConsumeHk(row).then(response => {
          this.formHk = response.data;
          this.formHk.id = row.id;
          this.formHk.remainder = row.remainder;
          this.classesHkList = response.data.classesList;
          this.openHk = true;
          this.titleHk = "设置耗课信息";
          this.setClasses();
        });
      } else if (id) {
        this.resetHkIds();    
        getConsumeHkByIds(id).then(response => {
          this.formHkIds = response.data;
          this.formHkIds.id = id;
          this.classesHkList = response.data.classesList;
          this.studentXYIdList = response.data.studentList;
          let xsIds = new Array()
          response.data.studentList.forEach(item =>{
            xsIds.push(item.studentId);  
          })
          this.formHkIds.studentId = xsIds;
          this.openHkIds = true;
          this.titleHkIds = "设置耗课信息";
          this.setClasses();
        });        
      } else {
        this.msgInfo("已没有剩余金额可用！");
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePayrecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPayrecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitFormHk() {
      this.$refs["formHk"].validate(valid => {
        if (valid) {
          if (this.formHk.consumeAmount>this.formHk.remainder) {
            this.msgInfo("剩余金额不足耗课");
          }else {
            addConsume(this.formHk).then(response => {
              this.$modal.msgSuccess("耗课设置成功");
              this.openHk = false;
              this.getList();
              this.expandKeys = [];
            });
          }
        }
      });
    },
    submitFormHkIds() {
      this.$refs["formHkIds"].validate(valid => {
        if (valid) {
          if (this.formHkIds.studentId.length<=0) {
            this.msgInfo("请选择学生");
          }else {
            let param = this.formHkIds;            
            param.studentIds = this.formHkIds.studentId;
            param.id = null;
            param.studentId = null;
            addConsume(param).then(response => {
              this.$modal.msgSuccess("耗课设置成功");
              this.openHkIds = false;
              this.getList();
              this.expandKeys = [];
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除缴费记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPayrecord(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有缴费记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPayrecord(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
<style scoped>
.sketch_content {
  overflow: auto;
  height: 600px;
  border-top: 1px solid #eff1f4;
  border-bottom: 1px solid #eff1f4;
  padding: 0px 0px 0px 0px;
}
.sketch_content::-webkit-scrollbar {
  width: 3px;
}
.sketch_content::-webkit-scrollbar-thumb {
  background: #8798AF;
  border-radius: 2px;
}
.sketch_content::-webkit-scrollbar-track {
  background: transparent;
}
</style>